/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxcostcentreapi.p                                */
/* PROCEDURE TYPE        : COSTCENTRE API Process                          */
/* DESCRIPTION           :                                                  */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                25-OCT-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}


{xxapilog.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.

{proxy/bcostcentredef.i}


define variable v_prefix                     as character        format "x(20)"     no-undo.
define variable v_extn                       as character                           no-undo.
define variable v_infile                     as character        format "X(40)"     no-undo.
define variable v_infile1                    as character        format "X(40)"     no-undo.
define variable v_file1                      as character        format "x(20)"     no-undo.
define variable v_file                       as character        format "x(50)"     no-undo.
define variable dir-file                     as character                           no-undo.
                                             
define variable m_arg1                       as character                           no-undo.
define variable v_tmpstr                     as character                           no-undo.
define variable v_tmpstr1                    as character                           no-undo.
define variable v_flcnt                      as integer                             no-undo.
define variable v_import                        as logical       init   "YES"       no-undo.
define stream   data-stream.                
 
define variable l_fdata                      as character                           no-undo.
define variable m_tmpstr                     as character        format "X(150)"    no-undo.
define variable v_cnt                        as integer    no-undo.
define variable lvi_row_id                   as integer     no-undo.
define variable v_unmtch_inv_ac              like GL.GLCode                         no-undo.
define variable v_dbcode                     as character                           no-undo.
define variable v_layerType1                 as character init "Primary"           no-undo.
define variable v_match_dbcode               as character                           no-undo.
define variable v_LayerType2                 as character init "Primary"           no-undo.
define variable v_dbSetCode                  as character no-undo.



/* ======== FOR API ========= */
define variable viReturn                     as integer     no-undo.
define variable ilReturnDataset              as logical     no-undo.
define variable ilPartialUpdate              as logical     no-undo.
define variable icPartialUpdateExceptionList as character   no-undo.
define variable ocPrimaryKey                 as character   no-undo.
define variable ocRowid                      as character   no-undo.
define variable oiDraftInstance              as integer     no-undo.
define variable ocPrimaryKeyName             as character   no-undo.
define variable vho                          as handle.
define variable oiReturnStatus               as integer     no-undo.
define buffer b_Currency for Currency.


define variable vi_filecount          as integer                  no-undo.
define variable vi_reccount           as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram         as character                 no-undo.
define variable vc_inv_account        as character                no-undo.
define variable vc_traceid            as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.


define temp-table tt_costcentre no-undo

   field  CostCentreCode              like CostCentre.CostCentreCode                    
   field  CostCentreDescription       like CostCentre.CostCentreDescription                 
   field  CostCentreIsActive          like CostCentre.CostCentreIsActive                 
   field  CostCentreIsWithSaf         like CostCentre.CostCentreIsWithSaf                
   field  CostCentreIsStructFromGL    like CostCentre.CostCentreIsStructFromGL           
   field  CostCentreSafSetOnOnDate    like CostCentre.CostCentreSafSetOnOnDate           
   field  tcBudgetGroupCode           as   character                                
   field  tlIsUsedInPosting           as   logical                                
   field  tcUsrLogin                  as   character   
   field  tcCCSafSetOnByUsrLogin      as   character
   field  tcCOAMaskCCCode             as   character
   field  tcSafStructureCode          as   character
   field  tc_Rowid                    as   character
   field  tc_ParentRowid              as   character                
   field  tc_Status                   as   character                
   field  CustomShort0                as   character                              
   field  CustomShort1                as   character
   field  CustomShort2                as   character
   field  CustomShort3                as   character
   field  dataid                      as   character
   field  traceid                     as   character
   field  billnbr                     as   character
   
   index idx1 CostCentreCode.
   



     DEFINE DATASET dscostcentre FOR tt_costcentre.
        

   hdataset = dataset dscostcentre:HANDLE.    
                
   
/** Get next rc_RowId fuction **/

function fiGetNextRowID returns character:
    lvi_row_id = lvi_row_id - 1.
    return string(string(lvi_row_id) + "row").
end function. 


/** Validation GCM setting for input output directory **/
  {us/xx/xxworkpath_e.i &outbound=YES &inbound=yes &archive=YES}
   
   m_workpath = m_workpath + "gl/".
   m_archpath = m_archpath + "gl/".
   m_outpath = m_outpath + "gl/".
   m_inpath = m_inpath + "gl/".


mainloop:
repeat on error undo mainloop, retry mainloop:
   
   
     
      empty temp-table tt_costcentre.
      empty temp-table api_status.
   
   
   v_runproagram = "".
   vc_traceid = "".

   /* Load data from JSON to temp-table */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
    
    find first tt_costcentre.
    assign vc_traceid = tt_costcentre.traceid.

   /* Pre-validation */   
 
   end. /*  if vl_RetOK then */

      
if vc_import and vl_RetOK and not can-find(first api_status where api_status.return_status  = "FAILED") then do:  
 
for each tt_costcentre break by tt_costcentre.CostCentreCode:
      
     create tCostCentre.
     buffer-copy tt_costcentre except dataid traceid billnbr to tcostcentre.
     
      
end. /* for each tt_costcentre */

   
      v_cnt = 0.
        
      run proxy/bcostcentre.p persistent set vhProxyComponent.
      
      /* DO THE VALIDATION BEFORE LOADING */
      run ApiMaintainByDatasetWithOutput in vhProxyComponent
      (input  current_entity,
      input  "save",
      input  true,
      input  true,
      input  icPartialUpdateExceptionList,
      input  dataset BCostCentre by-reference,
      output ocPrimaryKey,
      output ocRowid,
      output oiDraftInstance,
      output ocPrimaryKeyName,
      output dataset-handle vho,
      output dataset tFcMessages,
      output oiReturnStatus).


      delete procedure vhProxyComponent.
      
      v_cnt = 0.
      
        
   
      for each tFcMessages :
         if tFCMessages.tcFcType = "W" then next.
         if index(tFcMessages.tcFcMessage,"warning") <> 0 or
         index(tFcMessages.tcFcMessage, "The sum of the base and tax amounts") <> 0 then
         next.
        
         if  index(tFcMessages.tcFcMsgNumber, "qadfin-500498") <> 0 then
         next.

        
         v_cnt = v_cnt + 1.
         if v_cnt > 200 then
         leave.

         
         create api_status.
         assign api_status.data_seq         = v_cnt
            api_status.traceid          = tt_costcentre.traceid
            api_status.billnbr           = tt_costcentre.billnbr
            api_status.dataid           = tt_costcentre.dataid
            api_status.return_status    = "FAILED" 
            api_status.message_type     = "Import Validation"
         
            api_status.message_string   =  if tFcMessages.tcFcFieldName <> "" 
                                          then gettermlabel("SEVERITY",8) + " = " +
                                          string(tFcMessages.tiFcSeverity) + " | " +
                                          tFcMessages.tcFcMessage + " | " +
                                          gettermlabel("FIELD_NAME",10) + " = " +
                                          tFcMessages.tcFcFieldName + " | " +
                                          gettermlabel("FIELD_LABEL",11) + " = " +
                                          tFcMessages.tcFcFieldLabel + " | " +
                                          gettermlabel("FIELD_VALUE",11) + " = " +
                                          tFcMessages.tcFcFieldValue + " | " +
                                          tFcMessages.tcFcMsgNumber + " " + tFCMessages.tcFcType
                                          else 
                                          gettermlabel("SEVERITY",8) + " = " +
                                          string(tFcMessages.tiFcSeverity) + " | " +
                                          tFcMessages.tcFcMessage + " | " +
                                          tFcMessages.tcFcMsgNumber + " " + tFCMessages.tcFcType.
         
         
      end. /* for each tFcMessages : */
      

      if oiReturnStatus >= 0 then 
      do:
      
       create api_status.
         assign api_status.data_seq         = v_cnt
            api_status.traceid          = tt_costcentre.traceid
            api_status.billnbr           = tt_costcentre.billnbr
            api_status.dataid           = tt_costcentre.dataid
            api_status.return_status    = "SUCCESS" 
            api_status.message_type     = "Import Validation"
         
            api_status.message_string   =  "".
            
      end.
   
   /* Move input file to archive directory */
   
end. /*  if vc_import and vl_RetOK then do:   */
   
   
   
       for each tt_costcentre break by tt_costcentre.billnbr .
       if last-of(tt_costcentre.billnbr) then 
       do:
       vi_reccount = vi_reccount + 1.
              
       run CreateAPILogMaster(input vc_traceid,
                              input "createcostcentre",
                              input "xxcostcentreapi.p",
                              input "",
                              input "",
                              input vc_inputjson,
                              input tt_costcentre.billnbr,
                              input "",
                              input "",
                              input vi_reccount).  
                              
       end. /* if last-of(tt_CInvoiceReference.tt_CInvoiceReference) then  */
   
       end. /* for each tt_costcentre break by tt_costcentre.CostCentreCode */
   
   
   
   
          /* Handle cimload related files */
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          

          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */    


       
       leave.
       
end. /* repeat */





